package cn.edu.zut.pms.faker;

import com.github.javafaker.Faker;
import java.sql.*;
import java.util.Locale;
import java.util.Random;

public class FakerLogisticsDistrictGoodsCnt {
    public static void main(String[] args) {
        // 数据库连接信息（请根据实际环境修改）
        String url = "jdbc:mysql://192.168.17.150:3306/soil?allowPublicKeyRetrieval=true&useSSL=false";
        String user = "root";
        String password = "000000";

        // 创建中文Faker实例
        Faker faker = new Faker(new Locale("zh_CN"));
        Random random = new Random();

        // 定义销售区域（与源表保持一致的区域划分）
        String[] salesRegions = {"华北区", "华东区", "华南区", "西北区", "西南区", "东北区", "华中区"};

        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            // 先清空表中现有数据（可选）
            String truncateSql = "TRUNCATE TABLE ads_logistics_district_goods_cnt";
            try (Statement statement = connection.createStatement()) {
                statement.execute(truncateSql);
            }

            // 插入数据的SQL语句
            String insertSql = "INSERT INTO ads_logistics_district_goods_cnt (" +
                    "sales_region, quantity, sales_amount" +
                    ") VALUES (?, ?, ?)";

            try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql)) {
                for (String region : salesRegions) {
                    // 为每个区域生成合理的累计数量（根据区域规模差异设置不同范围）
                    int quantity;
                    switch (region) {
                        case "华东区":
                        case "华南区":
                            quantity = faker.number().numberBetween(5000, 10000); // 经济较发达区域数量更多
                            break;
                        case "华北区":
                        case "华中区":
                            quantity = faker.number().numberBetween(3000, 7000);
                            break;
                        default:
                            quantity = faker.number().numberBetween(1000, 4000); // 相对欠发达区域
                    }

                    // 生成销售金额（根据数量和合理单价范围计算，单价10-500元）
                    double avgPrice = faker.number().randomDouble(2, 10, 500);
                    double salesAmount = quantity * avgPrice;

                    // 设置参数
                    preparedStatement.setString(1, region);
                    preparedStatement.setInt(2, quantity);
                    preparedStatement.setDouble(3, salesAmount);

                    preparedStatement.executeUpdate();
                }
                System.out.println("数据插入完成，共插入 " + salesRegions.length + " 条区域统计记录");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}